package com.ceair.flight.test;

import com.ceair.flight.refund.dto.ota.RefundApplyReq;
import com.ceair.flight.refund.dto.ota.RefundApplyRes;
import com.ceair.flight.refund.service.ota.IRefundService;
import com.ceair.flight.refund.util.GsonUtil;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingOperationInfo;

import javax.xml.namespace.QName;

public class CxfClient {
        public static void main(String[] args) {
            cl1();
        }

        /**
         * 方式1.代理类工厂的方式,需要拿到对方的接口
         */
        public static void cl1() {
            try {
                // 接口地址
                String address = "http://localhost:8012/service/refundService?wsdl";
                // 代理工厂
                JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
                // 设置代理地址
                jaxWsProxyFactoryBean.setAddress(address);
                // 设置接口类型
                jaxWsProxyFactoryBean.setServiceClass(IRefundService.class);
                // 创建一个代理接口实现
                IRefundService refundService = (IRefundService) jaxWsProxyFactoryBean.create();
                // 数据准备
                RefundApplyReq req = new RefundApplyReq();
                req.setOrderNo("1019010800027201");
                req.setSalesChannel("7642");
                // 调用代理接口的方法调用并返回结果
                RefundApplyRes res = refundService.refundApply(req);
                String resultStr = GsonUtil.toJson(res);
                System.out.println("返回结果:" + resultStr);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        /**
         * 动态调用方式
         */
//        public static void cl2() {
//            // 创建动态客户端
//            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//            Client client = dcf.createClient("http://localhost:8012/service/refundService?wsdl");
//            // 需要密码的情况需要加上用户名和密码
//            // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
//            // PASS_WORD));
//            Object[] objects = new Object[0];
//            try {
//                // invoke("方法名",参数1,参数2,参数3....);
//                // 数据准备
//                RefundApplyReq req = new RefundApplyReq();
//                req.setOrderNo("1019010800027201");
//                req.setSalesChannel("7642");
//                String param = GsonUtil.toJson(req);
//                Endpoint endpoint = client.getEndpoint();
//                String operation = "refundApply";
//                QName opName = getOpName(client);
//                objects = client.invoke(operation, new Object[]{param});
//                System.out.println("返回数据:" + objects[0]);
//            } catch (java.lang.Exception e) {
//                e.printStackTrace();
//            }
//        }

    private static QName getOpName(Client client) {
        Endpoint endpoint = client.getEndpoint();
        String operation = "refundApply";
        QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), operation);
        BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding();
        if (bindingInfo.getOperation(opName) == null) {
            for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) {
                if (operation.equals(operationInfo.getName().getLocalPart())) {
                    opName = operationInfo.getName();
                    break;
                }
            }
        }

        return opName;
    }

}
